package org.fusionide.builder.parsers.cfml.tags;

import java.util.Vector;

import org.fusionide.builder.parsers.IParserHandler;
import org.fusionide.builder.parsers.ParserException;
import org.fusionide.builder.parsers.cfml.CFMLTag;
import org.fusionide.builder.parsers.cfml.Variable;


public class Catch extends CFMLTag {

	private String type;

	private boolean requireClosingTag = true;

	public String getType() {
		return type;
	}

	public void setType(String type) {
		this.type = type;
	}

	public Catch(String attributes, int startOffset) {
		super(attributes, startOffset);

		if (attributes == null)
			return;

		this.type = getAttribute("type");
	}

	@Override
	public boolean requiresClosingTag() {
		return requireClosingTag;
	}

	@Override
	public boolean acceptsChildren(CFMLTag tag) {
		return super.acceptsChildren(tag);
	}

	@Override
	public Vector<Variable> getVariables(int offset) {
		Vector<Variable> vars = new Vector<Variable>();
		if (offset >= getStartOffset() && offset <= getEndOffset()) {
			vars.add(new Variable("cfcatch", getEndLine(), true, Variable.struct));
			vars.add(new Variable("cfcatch.type", getEndLine(), true, Variable.string));
			vars.add(new Variable("cfcatch.message", getEndLine(), true, Variable.string));
			vars.add(new Variable("cfcatch.detail", getEndLine(), true, Variable.string));
			vars.add(new Variable("cfcatch.tagcontext", getEndLine(), true, Variable.array));
			vars.add(new Variable("cfcatch.nativeerrorcode", getEndLine(), true, Variable.string));
			vars.add(new Variable("cfcatch.sqlstate", getEndLine(), true, Variable.string));
			vars.add(new Variable("cfcatch.sql", getEndLine(), true, Variable.string));
			vars.add(new Variable("cfcatch.queryerror", getEndLine(), true, Variable.string));
			vars.add(new Variable("cfcatch.where", getEndLine(), true, Variable.string));
			vars.add(new Variable("cfcatch.errnumber", getEndLine(), true, Variable.string));
			vars.add(new Variable("cfcatch.missingfilename", getEndLine(), true, Variable.string));
			vars.add(new Variable("cfcatch.lockname", getEndLine(), true, Variable.string));
			vars.add(new Variable("cfcatch.lockoperation", getEndLine(), true, Variable.string));
			vars.add(new Variable("cfcatch.errorcode", getEndLine(), true, Variable.string));
			vars.add(new Variable("cfcatch.extendedinfo", getEndLine(), true, Variable.string));
			vars.addAll(super.getVariables(offset));
		}
		return vars;
	}

	@Override
	public boolean validate(IParserHandler parserHandler) {
		if (super.validate(parserHandler)) {
			if (!(hasParent() && getParent() instanceof Try)) {
				if (parserHandler != null)
					parserHandler.addMarker(new ParserException(
							"<cfcatch> Needs to have <cftry> as parent",
							ParserException.SEVERITY_ERROR,
							getStartLine()));
				return false;
			}
			return true;
		}
		return false;
	}

	@Override
	public void closeTag(int offset, int linenumber, IParserHandler parserHandler) {
		super.closeTag(offset, linenumber, parserHandler);
		requireClosingTag = false;
	}
}
